//
//  UserModel.m
//  WHSport
//
//  Created by ccjoy-jimneylee on 15/4/14.
//  Copyright (c) 2015年 jimneylee. All rights reserved.
//

#import "UserModel.h"
#import "WHClient.h"
#import "PlaceEntity.h"
#import "UserEntity.h"
#import "Util.h"
#import "WarEntity.h"
#import "PayClient.h"

@implementation UserModel{
    
    WHClient *_whClinet;
    PayClient *_payClient;
}


+ (UserModel *)sharedInstance{
    static UserModel *_sharedInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        _sharedInstance = [[UserModel alloc] init];
    });
    
    return _sharedInstance;
}

- (id)init {
    self = [super init];
    if (self) {
        _whClinet = [[WHClient alloc] init];
        _payClient = [[PayClient alloc] init];
    }
    return self;
}

- (BOOL)isOnline{
    return self.currentUser != nil;
}

- (void)loginWithLoginname:(NSString *)loginname
                  password:(NSString *)password
                 successed:(Successed)successed{
    NSString *wloginname =[loginname copy];
    NSString *wpassword = [password copy];
    NSDictionary *data = @{@"loginName":wloginname,
                           @"password":wpassword};
    [_whClinet POST:@"hhw-api/android/login"
         parameters:data
            success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSDictionary *result = [NSDictionary dictionaryWithDictionary:responseObject];
                NSNumber *success = responseObject[@"success"];
                NSDictionary *dateall = [NSDictionary dictionaryWithDictionary:[result objectForKey:@"data"]];
                
                if ([success unsignedIntValue] == 0) {
                    self.currentUser = [[UserEntity alloc] initWithAttributes:dateall];
                     NSString *idNumber = [dateall objectForKey:@"id"];
                    self.idNum = idNumber;
                    if (successed) {
                        successed(YES,0);
                    }
                }
                if ([success unsignedIntValue] == 1) {
                    if (successed) {
                        successed(NO,1);
                    }
                }

            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                 NSLog(@"the error is %@",error);
            }];
}

- (void)checkAccountIsExist:(NSString *)telephone
                successTele:(Successed)successTele{
    
    NSString *wteleph = [telephone copy];
    [_whClinet POST:@"hhw-api/CheckAccountIsExist"
         parameters:@{@"phone":wteleph}
            success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSDictionary *data = [NSDictionary dictionaryWithDictionary:responseObject];
                NSString *tele = [NSString stringWithFormat:@"%@",[data objectForKey:@"success"]];
                if ([tele isEqualToString:@"1"]) {
                    if (successTele) {
                        successTele(YES,1);
                    }
                }
                if ([tele isEqualToString:@"0"]) {
                    if (successTele) {
                        successTele(NO,0);
                    }
                }
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"the error is %@",error);
                if (successTele) {
                    successTele(NO,2);
                }
            }];


}

- (void)getSmsCode:(NSString *)telephone
    successTeleNum:(Successed)successTeleNum{
    NSString *teleNum = [telephone copy];
    [_whClinet GET:@"hhw-api/GetSmsCode"
        parameters:@{@"phone":teleNum}
           success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSLog(@"success");
           } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               NSLog(@"the error is %@",error);
               
           }];
}

- (void)checkSmsCode:(NSString *)telephone
             smscode:(NSString *)smscode
   successTeleNumber:(Successed)successTeleNumber{
    NSString *teleNum = [telephone copy];
    NSString *wsms = [smscode copy];
    NSDictionary *data = @{@"smsCode":wsms,
                           @"phone":teleNum};
    [_whClinet POST:@"hhw-api/CheckSmsCode"
         parameters:data
            success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSNumber *num = responseObject[@"success"];
                if ([num unsignedIntegerValue] == 0) {
                    if (successTeleNumber) {
                        successTeleNumber(YES,0);
                    }
                }
                if ([num unsignedIntegerValue] == 1) {
                    if (successTeleNumber) {
                        successTeleNumber(NO,1);
                    }
                }
  
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"the error is %@",error);
                
            }];
}

- (void)registWithLoginname:(NSString *)loginname
                   password:(NSString *)password
               successBlock:(Successed)successBlock{
    NSString *wloginname =[loginname copy];
    NSString *wpassword = [password copy];
    NSDictionary *data = @{@"loginName":wloginname,
                           @"password":wpassword};
    
    [_whClinet POST:@"hhw-api/regist"
         parameters:data
            success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSNumber *suc = responseObject[@"success"];
                if ([suc unsignedIntegerValue] == 0) {
                    if (successBlock) {
                        successBlock(YES,0);
                    }
                }
                if ([suc unsignedIntegerValue] == 1) {
                    if (successBlock) {
                        successBlock(NO,1);
                    }
                }
                
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"response message is %@", operation.responseString);
                NSLog(@"the error is %@",error);
                
            }];
}

- (void)personalModifyWithID:(NSString *)idNum
                         sex:(NSString *)sex
                     address:(NSString *)address
                        nick:(NSString *)nick
                      perMod:(Successed)perMod{
    NSString *pid = [idNum copy];
    NSString *psex = [sex copy];
    NSString *pnick = [nick copy];
    NSString *add = [address copy];
    NSDictionary *data = @{@"id":pid,@"sex":psex,@"nick":pnick,@"address":add};
    [_whClinet POST:@"hhw-api/UpdateAccount"
         parameters:data
            success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSNumber *num = responseObject[@"success"];
                if ([num unsignedIntegerValue] == 0) {
                    if (perMod) {
                        perMod(YES,0);
                        self.successPer =@"1";
                        [[NSNotificationCenter defaultCenter]
                         postNotificationName:@"NOTIFICATION_PERSONAL"
                                       object:nil];
                    }
                    [UserModel sharedInstance].currentUser.nick = pnick;
                    [UserModel sharedInstance].currentUser.sex = psex;
                    [UserModel sharedInstance].currentUser.address = add;
                }
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"%@",error);
            }];
}


- (void)personalModifyWithPassword:(NSString *)password
                                ID:(NSString *)Id
                            perPas:(Successed)perPasl{
    NSString *mid = [Id copy];
    NSString *mpassword = [password copy];
    NSDictionary *data = @{@"id":mid,
                           @"password":mpassword};
    [_whClinet POST:@"hhw-api/UpdatePassword"
         parameters:data
            success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSNumber *num = responseObject[@"success"];
                if ([num unsignedIntegerValue] == 0)  {
                    self.successPer = @"2";
                    if (perPasl) {
                        perPasl(YES,0);
                    }
                }
               
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"%@",error);
            }];


}

- (void)personalUpImgWithID:(NSString *)Id
                   filename:(NSString *)filename
                    imgdata:(NSData *)imgdata
                      perUp:(Successed)perUp{
    
    NSString *uid = [Id copy];
    NSString *uname = [filename copy];
    NSData *uimgdata = [imgdata copy];
    NSDictionary *data = @{@"id":uid,
                           @"fileName":uname};
    [_whClinet POST:@"hhw-api/headpic/upload"
         parameters:data
    constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFormData:uimgdata name:@"headpic"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSNumber *num = responseObject[@"success"];
        if ([num unsignedIntegerValue] == 0)  {
            if (perUp) {
                perUp(YES,0);
            }
            
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
        
    }];
}

- (void)findPasswordWithPhone:(NSString *)phone
                     password:(NSString *)password
                  successFind:(Successed)successFind{
    
    NSString *fphoto = [phone copy];
    NSString *fpassword = [password copy];
    NSDictionary *data = @{@"phone":fphoto,
                           @"password":fpassword};
    [_whClinet POST:@"hhw-api/FindPassword"
         parameters:data
            success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSNumber *num = responseObject[@"success"];
                if ([num unsignedIntegerValue] == 0)  {
                    if (successFind) {
                        successFind(YES,0);
                   }
                }
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                
            }];
}

- (void)GetMyEvaWithAccountId:(NSString *)accountid
                         page:(NSString *)page
                        MyEva:(SuccessedInfoBlock)MyEva{
    
    NSString *mid = [accountid copy];
    NSString *mpage = [page copy];
    NSDictionary *data = @{@"accountId":mid,
                           @"page":mpage};
    [_whClinet GET:@"hhw-api/GetEvaluationByAccount"
        parameters:data
           success:^(AFHTTPRequestOperation *operation, id responseObject) {
               NSMutableArray *array = [[NSMutableArray alloc] init];
               for (NSDictionary *dic in [responseObject objectForKey:@"data"]) {
                   PlaceEntity *placeEty = [[PlaceEntity alloc] initWithAttributes:dic];
                   [array addObject:placeEty];
               }
               MyEva(array);
               
           } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               NSLog(@"%@",error);
           }];
}

- (void)warJoinGetWithAccount:(NSString *)account
                       warGet:(SuccessedInfoBlock)warGet{
    NSString *idNum = [account copy];
    [_whClinet GET:@"hhw-api/GetApplyYueZhan"
        parameters:@{@"accountId":idNum}
           success:^(AFHTTPRequestOperation *operation, id responseObject) {
               NSMutableArray *array = [[NSMutableArray alloc] init];
               for (NSDictionary *dic in [responseObject objectForKey:@"data"]) {
                   WarEntity *warEty = [[WarEntity alloc] initWithAttributes:dic];
                   [array addObject:warEty];
               }
               warGet(array);
           } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               NSLog(@"%@",error);
           }];
}

- (void)warSendGetWithAccount:(NSString *)account
                       warGet:(SuccessedInfoBlock)warGet{
    NSString *idNum = [account copy];
    [_whClinet GET:@"hhw-api/GetYueZhanInfo"
        parameters:@{@"accountId":idNum}
           success:^(AFHTTPRequestOperation *operation, id responseObject) {
               NSMutableArray *array = [[NSMutableArray alloc] init];
               for (NSDictionary *dic in [responseObject objectForKey:@"data"]) {
                   WarEntity *warEty = [[WarEntity alloc] initWithAttributes:dic];
                   [array addObject:warEty];
               }
               warGet(array);
           } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               NSLog(@"%@",error);
           }];
}

- (void)myfriendListWithID:(NSString *)inNum
                  myfriend:(SuccessedInfoBlock)myfriend{
    
    NSString *friend = [inNum copy];
    [_whClinet GET:@"hhw-api/GetMyFriend"
        parameters:@{@"id":friend}
           success:^(AFHTTPRequestOperation *operation, id responseObject) {
               NSMutableArray *array = [[NSMutableArray alloc] init];
               for (NSDictionary *dic in [responseObject objectForKey:@"data"]) {
                   UserEntity *usEty = [[UserEntity alloc] initWithAttributes:dic];
                   [array addObject:usEty];
               }
               myfriend(array);
               
           } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               NSLog(@"%@",error);
           }];
}

- (void)myfriendDeleteWithID:(NSString *)idNum
                      Delete:(Successed)Delete{
    NSString *did = [idNum copy];
    [_whClinet POST:@"hhw-api/DeleteFriend"
         parameters:@{@"id":did}
            success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSNumber *num = responseObject[@"success"];
                if ([num unsignedIntegerValue] == 0)  {
                    if (Delete) {
                        Delete(YES,0);
                    }
                }
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"%@",error);
            }];
    
}

- (void)sendCommtWithAccount:(NSString *)idNum
                    sportsID:(NSString *)sportID
                        cont:(NSString *)cont
                       level:(NSString *)level
                     getSend:(Successed)getSend{
    NSString *aid = [idNum copy];
    NSString *asid = [sportID copy];
    NSString *acont = [cont copy];
    NSString *alevel = [level copy];
    NSDictionary *data = @{@"accountId":aid,@"sportsHallId":asid,
                               @"content":acont,@"level":alevel};
    [_whClinet POST:@"hhw-api/SetEvaluation"
         parameters:data
            success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSNumber *num = responseObject[@"success"];
                if ([num unsignedIntegerValue] == 0)  {
                    if (getSend) {
                        getSend(YES,0);
                    }
                }
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"%@",error);
            }];
}

- (void)changeOrderWithID:(NSString *)idNum
                   status:(NSString *)status
                  success:(Successed)success{
    NSString *cid = [idNum copy];
    NSString *cstatus = [status copy];
    NSDictionary *data = @{@"id":cid,@"status":cstatus};
    [_whClinet POST:@"hhw-api/UpdateOrderStatus"
         parameters:data
            success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSNumber *num = responseObject[@"success"];
                if ([num unsignedIntegerValue] == 0)  {
                    if (success) {
                        success(YES,0);
                    }
                }
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"%@",error);
            }];
}

- (void)paymoneyWithMoney:(NSString *)money
                 ordernum:(NSString *)ordernum
                  subject:(NSString *)subject
                     body:(NSString *)body
               paysuccess:(SuccessedInfoBlock)paysuccess{
    NSString *pay = [money copy];
    NSString *orderNum = [ordernum copy];
    NSString *psub = [subject copy];
    NSString *pbody = [body copy];
//    NSDictionary *data = @{@"money":pay,@"OrderNum":orderNum,
//                           @"Subject":psub,@"Body":pbody};
    [_payClient POST:@"hhw-api/qr.jsp"
          parameters:@{@"money":pay,@"OrderNum":orderNum,
                       @"Subject":psub,@"Body":pbody}
            success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSData *data = responseObject;
                paysuccess(data);
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"%@",operation.responseString);
            }];
    
}

- (void)getWarDetailWithWarid:(NSString *)warNum
                       userid:(NSString *)userid
                      getData:(SuccessedInfoBlock)getData{
    NSString *awar = [warNum copy];
    NSString *aid = [userid copy];
    NSDictionary *data = @{@"id":awar,@"accountId":aid};
    [_whClinet GET:@"hhw-api/GetYueZhanDetail"
        parameters:data
           success:^(AFHTTPRequestOperation *operation, id responseObject) {
               NSArray *array = responseObject[@"data"];
               getData(array);
           } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               NSLog(@"%@",error);
           }];
}

- (void)addFriendWithmeID:(NSString *)meID
                  otherID:(NSString *)otherID
               returnData:(Successed)returnData{
    NSString *amid = [meID copy];
    NSString *aoid = [otherID copy];
    NSDictionary *data = @{@"accountAId":amid,@"accountBId":aoid};
    [_whClinet POST:@"hhw-api/AddFriend"
         parameters:data
            success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSNumber *num = responseObject[@"success"];
                if ([num unsignedIntegerValue] == 0)  {
                    if (returnData) {
                        returnData(YES,0);
                    }
                }
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"%@",error);
            }];
}

- (void)applyWarWithMeid:(NSString *)Meid
                   warID:(NSString *)warID
                 appData:(Successed)appData{
    NSString *amid = [Meid copy];
    NSString *awid = [warID copy];
    NSDictionary *data = @{@"accountId":amid,@"yuezhanId":awid};
    [_whClinet POST:@"hhw-api/SetYueZhanApply"
         parameters:data
            success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSNumber *num = responseObject[@"success"];
                if ([num unsignedIntegerValue] == 0)  {
                    if (appData) {
                        appData(YES,0);
                    }
                }
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"%@",error);
            }];
}

- (void)mywarPersonWithwid:(NSString *)wid
                    myData:(SuccessedInfoBlock)myData{
    NSString *mwid = [wid copy];
    [_whClinet GET:@"hhw-api/GetApplyAccount"
        parameters:@{@"yuezhanId":mwid}
           success:^(AFHTTPRequestOperation *operation, id responseObject) {
               NSMutableArray *array = [[NSMutableArray alloc] init];
               for (NSDictionary *dic in [responseObject objectForKey:@"data"]) {
                   WarEntity *warEty = [[WarEntity alloc] initWithAttributes:dic];
                   [array addObject:warEty];
               }
               myData(array);
           } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               NSLog(@"%@",error);
           }];
}

- (void)sendWarWithaccountId:(NSString *)accountId
                sportEventId:(NSString *)sportEventId
                sportsHallId:(NSString *)sportsHallId
                   timeLimit:(NSString *)timeLimit
                       count:(NSString *)count
                       level:(NSString *)level
                      remark:(NSString *)remark
                    fileName:(NSString *)fileName
                  yuezhanpic:(NSData *)yuezhanpic
                     warData:(Successed)warData{
    
    NSString *sid = [accountId copy];
    NSString *seid = [sportEventId copy];
    NSString *shid = [sportsHallId copy];
    NSString *stime = [timeLimit copy];
    NSString *scount = [count copy];
    NSString *slevel = [level copy];
    NSString *sremark = [remark copy];
    NSString *sfile = [fileName copy];
    NSData *simg = [yuezhanpic copy];
    NSDictionary *data = @{@"remark":sremark,@"sportEventId":seid,
                           @"count":scount,@"accountId":sid,
                           @"timeLimit":stime,@"fileName":sfile,
                           @"sportsHallId":shid,@"level":slevel};
    [_whClinet POST:@"hhw-api/SetYueZhanInfo"
         parameters:data
     constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
         [formData appendPartWithFormData:simg name:@"yuezhanpic"];
     } success:^(AFHTTPRequestOperation *operation, id responseObject) {
         NSNumber *num = responseObject[@"success"];
         if ([num unsignedIntegerValue] == 0)  {
             if (warData) {
                 warData(YES,0);
             }
         }
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"%@",error);
     }];
}

- (void)setApplrWithwarID:(NSString *)warID
                  applyid:(NSString *)applyid
                   status:(NSString *)status
                getStatus:(Successed)getStatus{
    
    NSString *idNum = [warID copy];
    NSString *appid = [applyid copy];
    NSString *astatus = [status copy];
    NSDictionary *data =@{@"yuezhanId":idNum,@"id":appid,
                          @"status":astatus};
    [_whClinet POST:@"hhw-api/SetSelectApply"
         parameters:data
            success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSNumber *num = responseObject[@"success"];
                if ([num unsignedIntegerValue] == 0)  {
                    if (getStatus) {
                        getStatus(YES,0);
                    }
                }
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"%@",error);
            }];


}


@end
